package com.example.crud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.crud.mapper.AdminMapper;
import com.example.crud.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * ClassName: AdminController
 * Package: com.example.crud.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-15
 * Time: 14:32
 */

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminMapper adminMapper;

    //查询admin所有数据
    @RequestMapping("/selectAll")
    public List<Admin> selectAll(){
        return adminMapper.selectList(null);
    }

    //根据id来查询
    @RequestMapping("/selectById")
    public Admin selectById(){
        return adminMapper.selectById(101);
    }

    //根据id批量查询
    @RequestMapping("/ids")
    public List<Admin> ids(){
        List<Integer> list = new ArrayList<>();
        list.add(101);
        list.add(102);
        return adminMapper.selectBatchIds(list);
        //Batch 批量的意思
    }

    //根据map条件查询
    @RequestMapping("/map")
    public List<Admin> map(){
        //SELECT id,name,gender,password,email,telephone,address,portrait_path FROM tb_admin
        // WHERE name = ? AND id = ?  map添加的条件同时满足 中间是and
        Map<String,Object> map = new HashMap<>();
        map.put("id",101);
        map.put("name","admin");
        List<Admin> list = adminMapper.selectByMap(map);
        list.forEach(System.out::println);
        return list;
        //单个map是根据Map集合条件查询 maps是条件构造器
    }

    //maps
    @RequestMapping("/maps")
    public List<Map<String,Object>> maps(){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        //在select查询指定字段
        queryWrapper.select("name","id","email");
        List<Map<String,Object>> maps = adminMapper.selectMaps(queryWrapper);
        maps.forEach(System.out::println);
        return maps;
    }

    //根据实体条件 查询一条记录
    @RequestMapping("/one")
    public Admin one(){
        //配合条件构造器使用 返回的是一条记录 那么查也是查一条记录 返回多条就报错
        //Query是查询的意思 Wrapper是包装器的意思 就是条件构造器
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id",104);
        //查询id大于等于104的数据
        return adminMapper.selectOne(queryWrapper);
    }

    //selectList 根据条件查询全部记录
    @RequestMapping("/all")
    public List<Admin> all(){
        //查询id大于等于 102 并且降序排列
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id",102).orderByDesc("id");
        //desc 降序
        return adminMapper.selectList(queryWrapper);
    }


    // 返回第一个一个字段的值
    @RequestMapping("/obj")
    public List<Object> obj(){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("email");
        return adminMapper.selectObjs(queryWrapper);
        //查询都能查询 但是也只返回第一个字段的值
    }



    //  增加数据 BaseMapper 中就一个增加数据 一个实体类
    @RequestMapping("/insert")
    public void insert(){
        //
        Admin admin = new Admin();
        admin.setName("小明");
        admin.setEmail("qwe@com.com");
        int ret = adminMapper.insert(admin);
        System.out.println(ret);
    }


    //删除 delete
    //根据id进行删除
    @RequestMapping("/byId")
    public void byId(){
        int ret = adminMapper.deleteById(157);
        System.out.println(ret);
    }


    //根据实体id删除
    //实际业务中可以获取到实体对象 然后匹配到实体对象id删除
    @RequestMapping("/pojoId")
    public void pojoId(){
        Admin admin = new Admin();
        admin.setId(101);
        int ret = adminMapper.deleteById(admin);
        System.out.println(ret);
    }

    //根据条件构造器删除
    @RequestMapping("/dele")
    public void delete(){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id",104);
        int ret = adminMapper.delete(queryWrapper);
        System.out.println(ret);
    }





}
